package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyAttendantDao;
import ex.com.jm.food.entity.Attendant;
import ex.com.jm.food.entity.Streets;
/**
 * dao接口实现
 * @author zhaojw
 *
 */
public class CompanyAttendantDaoImpl extends HibernateDaoSupport implements
		CompanyAttendantDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Attendant> getAttendants(final int initPage,final int pageRow,
			final String comId,final String attName,final Integer status,final String order,final String sort) {
		// TODO Auto-generated method stub
		Object atts=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.AttendantDTO("
						+ "att.attId,"
						+ "att.attName,"
						+ "att.attAddress,"
						+ "att.status,"
						+ "att.created"
						+ ") from Attendant att where att.comId='"+comId+"' ";
				if (attName!=null&&!attName.equals("")) {
					hql+=" and att.attName like '%"+attName+"%'";
				}
				if(status!=null&&!status.equals("")){
					hql+=" and att.status="+status;
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by att."+order+" "+sort;
				}else{
					hql+=" order by att.created desc ";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Attendant>) atts;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int attCount(final String comId,final String attName,final Integer status) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Attendant att where att.comId='"+comId+"' ";
				if (attName!=null&&!attName.equals("")) {
					hql+=" and att.attName like '%"+attName+"%'";
				}
				if(status!=null&&!status.equals("")){
					hql+=" and att.status="+status;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateStatus(final Integer status,final String attId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Attendant set status="+status+" where attId='"+attId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
		if(count==1){
			return true;
		}
		return false;
	}

	@Override
	public Object saveAtten(Attendant attendant) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(attendant);
	}

	@Override
	public void updateAtten(Attendant attendant) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().update(attendant);
	}

	@Override
	public void delAtten(String attId) {
		// TODO Auto-generated method stub
        this.getHibernateTemplate().delete(this.getHibernateTemplate().get(Attendant.class, attId));
	}

	@Override
	public Attendant editAttendant(String attId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Attendant.class, attId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Attendant> getAttens(final String comId,final String address) {
		// TODO Auto-generated method stub
		Object atts=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from Attendant att where att.comId='"+comId+"' and att.status=0 and att.attAddress like '%"+address+"%'";
				Query query=session.createQuery(hql);
				return query.list();
			}
		});
		return (List<Attendant>) atts;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Streets> getStreets(final String county) {
		// TODO Auto-generated method stub
		Object streets=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createSQLQuery("select * from jm_streets street join jm_countys as county on street.county_id=county.county_id  where county_name='"+county+"'").addEntity("streets",Streets.class);
				return query.list();
			}
		});
		return (List<Streets>) streets;
	}

}
